package risk.control.application.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import java.io.Serializable;
import java.util.Map;

public class RouteJump {

    public static final String LOGIN_ACTION = "function.main.LoginActivity";//登录页面
    public static final String MAIN_ACTION = "function.main.MainActivity";//主页
    public static final String MAKE_ACTION = "function.main.MakeActivity";//预约页面
    public static final String MYSELF_ACTION = "function.main.MyselfActivity";//本人/代办人页面
    public static final String QUERY_RESULT_ACTION = "function.main.QueryResultActivity";//查询结果页
    public static final String MORE_ACTION = "function.main.MoreActivity";//更多页

    /**
     * @param activity    activity
     * @param action      action
     * @param params      params
     * @param isFinish    true->关闭当前页
     * @param isAnimation true->有动画
     */
    public static void start(Activity activity, String action, Map<String, Object> params, boolean isFinish, boolean isAnimation) {
        Intent intent = new Intent();
        intent.setAction(action);
        addParamsToIntent(intent, params);
        activity.startActivity(intent);
        if (!isAnimation) {
            //没有动画
            activity.overridePendingTransition(0, 0);
        }
        if (isFinish) {
            //结束页面
            activity.finish();
        }
    }


    /**
     * 添加参数到Intent
     *
     * @param intent 目标Intent
     * @param params 参数Map
     */
    private static void addParamsToIntent(Intent intent, Map<String, Object> params) {
        if (intent == null || params == null) {
            return;
        }
        Bundle bundle = new Bundle();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Integer) {
                bundle.putInt(key, (Integer) value);
            } else if (value instanceof String) {
                bundle.putString(key, (String) value);
            } else if (value instanceof Boolean) {
                bundle.putBoolean(key, (Boolean) value);
            } else if (value instanceof Float) {
                bundle.putFloat(key, (Float) value);
            } else if (value instanceof Double) {
                bundle.putDouble(key, (Double) value);
            } else if (value instanceof Long) {
                bundle.putLong(key, (Long) value);
            } else if (value instanceof Parcelable) {
                bundle.putParcelable(key, (Parcelable) value);
            } else if (value instanceof Serializable) {
                bundle.putSerializable(key, (Serializable) value);
            }
        }
        intent.putExtras(bundle);
    }

}
